
package com.scjyzr.mysweeper;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class MyUtil {

    public static AlertDialog showOkCancle(Context activity, final MyCallBack callback,String message,final int flag) {
        return new AlertDialog.Builder(activity)
                .setIcon(R.drawable.bomb)
                .setTitle(R.string.title)
                .setMessage(message)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if (callback != null) {
                            callback.myCallBack(null,flag);
                        }

                    }
                }).create();
//                .setNegativeButton(R.string.alert_dialog_cancel,
//                        new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int whichButton) {
//                                if (callback != null) {
//                                    callback.myCallBack(flag);
//                                }
//                            }
//                        }).create();
    }

    /**
     * 选择难度对话框。
     * 
     * @param activity
     * @return
     */
    public static AlertDialog showDiffDialogForSelect(final GameMainActivity activity) {

        AlertDialog myDialog = new AlertDialog.Builder(activity)
                .setIcon(R.drawable.bomb)
                .setTitle(R.string.diffict_select)
                .setSingleChoiceItems(R.array.select_dialog_diff_level,
                        MyUtil.getDiffLevel(activity), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                activity.setCurrentSelect(whichButton);
                            }
                        })
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        saveDiffLevel(activity.getCurrentSelect(), activity);
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                activity.resetCurrentDiffLevel();

                            }
                        }).create();
        return myDialog;
    }

    /**
     * 把当前难度设置在游戏中。
     * 
     * @return
     */
    public static void saveDiffLevel(int level, Context context) {
        StatisiticUtil.functiontSatistics(context, StatisiticUtil.StatisticKey.SELECT_DIFF_OK);
        SharedPreferences hand = context.getSharedPreferences(DIFF_KEY, Context.MODE_PRIVATE);
        Editor editer = hand.edit();
        editer.putInt(DIFF_KEY, level);
        editer.commit();
    }

    /**
     * 获取当前游戏难度。
     * 
     * @return
     */
    public static int getDiffLevel(Context context) {
        SharedPreferences hand = context.getSharedPreferences(DIFF_KEY, Context.MODE_PRIVATE);
        return hand.getInt(DIFF_KEY, EASY);

    }
    
    
    

    public static void saveScore(int score, Context context) {
        SharedPreferences hand = context.getSharedPreferences(SCORE, Context.MODE_PRIVATE);
        Editor editer = hand.edit();
        editer.putInt(SCORE, score);
        editer.commit();
    }


    public static int getScore(Context context) {
        SharedPreferences hand = context.getSharedPreferences(SCORE, Context.MODE_PRIVATE);
        return hand.getInt(SCORE, 0);

    }

    /**
     * 获取当前地雷的数量。
     * 
     * @param context
     * @return
     */
    public static int getCurrentMineNumber(Context context,int total) {
        int size=(10 * (getDiffLevel(context) + 1)*total)/100;
        Log.i("scj", "总的地雷数量是 "+size);
        return size;
    }

    private static final String DIFF_KEY = "diff_key";
    
    private static final String SCORE = "score";

    public static final int EASY = 0;

    public static final int MIDD = 1;

    public static final int DIFF = 2;
}
